<template>
  <div class="app-container">
    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="项目类型" prop="projectId">
        <el-select
          v-model="queryParams.projectId"
          placeholder="全部"
          @change="handleQuery"
        >
          <el-option
            v-for="item in projectList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="培训名称" prop="trainName">
        <el-input
          v-model.trim="queryParams.trainName"
          placeholder="培训名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训时间" prop="trainTime">
        <el-date-picker
          v-model="trainTimeArr"
          type="datetimerange"
          align="right"
          value-format="YYYY-MM-DD HH:mm:ss"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="报名时间" prop="enrollTime">
        <el-date-picker
          v-model="enrollTimeArr"
          type="datetimerange"
          align="right"
          value-format="YYYY-MM-DD HH:mm:ss"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--表格数据-->
    <el-table v-loading="loading" :data="tableData" @sort-change="sortChange">
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column
        label="培训名称"
        prop="trainName"
        min-width="100"
        align="center"
      />
      <el-table-column
        label="项目类型"
        prop="projectName"
        min-width="100"
        align="center"
      />
      <el-table-column
        label="培训项目"
        prop="projects"
        min-width="200"
        align="center"
      />
      <el-table-column
        label="培训时间"
        prop="trainTimeStr"
        min-width="270"
        align="center"
        :sortable="'custom'"
      />
      <el-table-column
        label="报名时间"
        prop="enrollTimeStr"
        min-width="270"
        align="center"
        :sortable="'custom'"
      />
      <el-table-column
        label="报名人数"
        prop="enrollCounts"
        min-width="120"
        align="center"
      />
      <el-table-column
        label="待审核人数"
        prop="waitVerifies"
        min-width="120"
        align="center"
      />
      <el-table-column
        label="审核通过人数"
        prop="isVerifies"
        min-width="120"
        align="center"
      />
      <el-table-column
        align="center"
        label="审核不通过人数"
        prop="noVerifies"
        min-width="120"
      />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding"
        min-width="160"
      >
        <template #default="scope">
          <el-button type="text" @click="goEnrollVerity(scope.row)"
            >报名审核</el-button
          >
          <el-button type="text" @click="enrollDelay(scope.row)"
            >报名延期</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <dialogEnrollDelay :props="dialogDelayProps" @submitForm="resetQuery" />
  </div>
</template>

<script setup name="verity">
import { getCurrentInstance, ref } from "vue";
import { onMounted, reactive } from "@vue/runtime-core";
import { toRefs } from "@vueuse/shared";
import { getTrainsList } from "../../../api/train/verity";
import { getBaseProjectList } from "../../../api/oam/basic";
import { useRouter } from "vue-router";
import dialogEnrollDelay from "./components/dialog/enrollDelay";

const router = useRouter();
const { proxy } = getCurrentInstance();

const showSearch = ref(true);
const trainTimeArr = ref([]);
const enrollTimeArr = ref([]);
const tableData = ref([]);
const total = ref(0);
const loading = ref(true);
const projectList = ref([]);
const dialogPropsBase = {
  open: false,
  title: "",
  data: null,
};
const dialogDelayProps = ref({
  ...dialogPropsBase,
});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: undefined,
    trainName: undefined,
    trainOrder: 0,
    signOrder: 0,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

//获取列表
function getList() {
  loading.value = true;
  if (trainTimeArr.value) {
    queryParams.value.trainTimeRange = trainTimeArr.value.toString();
  }
  if (enrollTimeArr.value) {
    queryParams.value.enrollTimeRange = enrollTimeArr.value.toString();
  }
  getTrainsList(queryParams.value).then((res) => {
    loading.value = false;
    tableData.value = res.rows;
    total.value = res.total;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  trainTimeArr.value = [];
  enrollTimeArr.value = [];
  handleQuery();
}

function getProjectList() {
  getBaseProjectList().then((res) => {
    projectList.value = res.rows;
  });
}

//报名延期
function enrollDelay(row) {
  _.assign(dialogDelayProps.value, dialogPropsBase, {
    open: true,
    isView: true,
    title: "报名延期",
    data: { ...row },
  });
}

//报名审核
function goEnrollVerity(row) {
  router.push({ name: "personVerity", params: { trainId: row.trainId } });
}

onMounted(() => {
  getList();
  getProjectList();
});

// 降序升序
function sortChange(column) {
  // 升序
  if (column.prop == "trainTimeStr") {
    // 报名
    if (column.order == "ascending") {
      queryParams.value.signOrder = 1;
    } else if (column.order == "descending") {
      queryParams.value.signOrder = 0;
    }
  }

  if (column.prop == "matchTimeStr") {
    // console.log(column.prop);
    // 赛事
    if (column.order == "ascending") {
      queryParams.value.trainOrder = 1;
    } else if (column.order == "descending") {
      queryParams.value.trainOrder = 0;
    }
  }

  getList();
}
</script>

<style scoped></style>
